import cn.com.kh.KHDBConfig;
import cn.com.kh.KHUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @projectName: ngsapitask-hst
 * @package: PACKAGE_NAME
 * @className: DB_Do1
 * @author: LiJT
 * @description: TODO
 * @date: 2025-04-13 23:15
 * @version: 1.0
 */
public class DB_Do1 {
    private static final Log log = LogFactory.getLog(DB_Do1.class);

    public static void main(String[] args) {

        // 获取数据源
        JdbcTemplate jt = new JdbcTemplate(TxConfig.getDruidDataSource());

        int remind;
        try {

            //设置日期格式
            SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
            Calendar today = Calendar.getInstance();
            log.debug("系统当前日期:" + dft.format(today.getTime().getTime()));

            Date now_Time = df.parse(df.format(new Date()));
            log.debug("系统当前时间:" + df.format(now_Time));

            // String workgroup_id = request.getParameter("workgroupId");
            String workgroup_id = "1";
            log.debug("工作组ID:" + workgroup_id);

            // 昨天
            String old_Date_SQL = "Select Date_Format(Freedate, '%Y-%m-%d') Worktimeflag From c_Freedate_Chat c Where Date_Sub(Curdate(), Interval 1 Day) = Date_Format(Freedate, '%Y-%m-%d') And (c.Workgroup_Id = ? Or c.Workgroup_Id = '' Or c.Workgroup_Id Is Null) Limit 1";
            List<Map<String, Object>> old_Date_List = jt.queryForList(old_Date_SQL, workgroup_id);
            log.debug("昨天-节假日查询结果1:" + old_Date_List.toString());

            // 当天
            String now_Date_SQL = "Select Date_Format(Freedate, '%Y-%m-%d') worktimeflag From c_Freedate_Chat c Where Current_Date = Date_Format(Freedate, '%Y-%m-%d') And (c.Workgroup_Id = ? Or c.Workgroup_Id = '' Or c.Workgroup_Id Is Null) Limit 1";
            List<Map<String, Object>> now_Date_List = jt.queryForList(now_Date_SQL, workgroup_id);
            log.debug("当天-节假日查询结果:" + now_Date_List.toString());

            // 时间类型识别
            boolean is_WorkTime = true;
            try {
                // 1-工作日->非工作日 00:00:00~18:00:00
                // 2-非工作日->非工作日 09:00:00~18:00:00
                // 3-非工作日->工作日 08:00:00~24:00:00
                // 4-工作日->工作日 00:00:00~24:00:00
                String time_Type = "";
                if (old_Date_List.size() == 0 && now_Date_List.size() > 0) {
                    time_Type = "1";
                } else if (old_Date_List.size() > 0 && now_Date_List.size() > 0) {
                    time_Type = "2";
                } else if (old_Date_List.size() > 0 && now_Date_List.size() == 0) {
                    time_Type = "3";
                } else {
                    time_Type = "4";
                }
                log.debug("时间类型识别 识别类型:" + time_Type);
                String time_Conf = KHDBConfig.getParamConfig(jt, "ChatWork_Time_" + time_Type);
                time_Conf = KHUtil.isNull(time_Conf) ? "09:00:00~18:00:00" : time_Conf;
                log.debug("时间类型识别 类型时间配置:" + time_Conf);

                String[] time_Arr = time_Conf.split(";");
                for (int i = 0; i < time_Arr.length; i++) {
                    log.debug("时间类型识别 Loop校验时间片段:" + time_Arr[i]);
                    String[] time_Part = time_Arr[i].split("~");
                    Date startTime = df.parse(time_Part[0]);
                    Date endTime = df.parse(time_Part[1]);

                    is_WorkTime = belongCalendar(now_Time, startTime, endTime);
                    log.debug("时间类型识别 Loop校验时间片段:" + time_Arr[i] + " 判断当前时间是否在时间片段中:" + is_WorkTime);

                    if (is_WorkTime) {
                        break;
                    }

                }
                log.debug("当前时间是否非工作时间检查结果:" + is_WorkTime);
            } catch (Exception e) {
                log.error("时间类型识别 异常", e);
            }

            //校验标识
            if (!is_WorkTime) {
                log.debug("当前为非工作时间");
                remind = 0;
            } else {
                log.debug("当前是工作时间，返回-1检查工作组工作时间");
                remind = -1;
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            remind = -1;
        }
        log.debug("是否非工作时间检查结果:" + remind);

        // return remind;
    }

    /**
     * 判断时间是否在时间段内
     *
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {

        if (nowTime.getTime() == beginTime.getTime() || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }
}
